<?php
ini_set('max_execution_time', 3000);
include 'lib/LIB_parse.php';
include 'lib/LIB_http.php';


function labelPapers($papers, $keywords)

{
$loginUrl = 'https://login.www.libproxy.wvu.edu/login?qurl=http%3a%2f%2fwww.ieee.org%2fieeexplore';
$fields = 'user=jgoble&pass=Sl33pwThemA77';
$remotePageUrl = 'http://ieeexplore.ieee.org.www.libproxy.wvu.edu/stamp/stamp.jsp?tp=&arnumber=134075';

$login = getUrl($loginUrl, 'post', $fields); //login to the site

$validArray = array();
$notFoundArray = array();

foreach ($papers as $paper) 
{
    $remotePageUrl = makeIEEEurl($paper->arnumber);
    
   
    $remotePage = getUrl($remotePageUrl); //get the remote page
    //echo $remotePage;
    //break;
    $tagArray = parse_array($remotePage, "<frame", ">");

    $pdfUrl = get_attribute($tagArray[2], $attribute="src"); 

    $actualPDF = getUrl($pdfUrl);
   
    
    $myFile = "/xpdf/testFile.pdf";
    $fh = fopen($myFile, 'w');
    fwrite($fh, $actualPDF);
    fclose($fh);

    $content = shell_exec('\\xpdf\\pdftotext '.$myFile.' -');

    foreach ($keywords as $keyword) 
    {
        $result = stripos($content, $keyword);
        if ($result)
            break; //else keep loking
    }

if ($result) {
    array_push($validArray, $paper);
}
else 
{
    array_push($notFoundArray, $paper);
}

}
    $returnVal = new returnObj;
    $returnVal->valid = $validArray;
    $returnVal->invalid = $notFoundArray;
    
    return $returnVal;
    

}

function makeIEEEurl ($arnumber) 
{
    return "http://ieeexplore.ieee.org.www.libproxy.wvu.edu/stamp/stamp.jsp?tp=&arnumber=" . $arnumber;
}


function getUrl($url, $method='', $vars='') {
    $ch = curl_init();
     
    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    
    $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);   
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    
    $buffer = curl_exec($ch);
    if (curl_errno($ch))
    echo 'Curl error: ' . curl_error($ch);
    
    $buffer = curl_exec($ch);
  
    //print_r(curl_getinfo($ch));

    
    curl_close($ch);
    return $buffer;
}

class returnObj 
{
    public $valid, $invalid, $notInRepos;
}


?>